package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    /**
     * 显示页面
     *
     * @param model
     * @return
     */
    @RequestMapping
    public String index(Model model) {
        List<Destination> hotRegions = regionService.listHotRegion();
        model.addAttribute("hotRegions", hotRegions);
        return "destination/index";
    }

    /**
     * 查询目的地切换
     *
     * @param regionId 地区id
     * @param model    模型
     * @return
     */
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(long regionId, Model model) {
        List<Destination> destinations = destinationService.listByParentId(regionId);
        int size = destinations.size();
        //leftDests
        if (size > 1) {
            model.addAttribute("leftDests", destinations.subList(0, size / 2 + 1));
            //rightDests
            model.addAttribute("rightDests", destinations.subList(size / 2 + 1, size));
        }else {
            model.addAttribute("leftDests", destinations);
        }

        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    /**
     * 目的地页面跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("guide")
    public String guide(Long id, Model model) {
        //吐司操作
        List<Destination> list = destinationService.creatToasts(id);
        Destination dest = list.remove(list.size() - 1);
        model.addAttribute("toasts", list);
        model.addAttribute("dest", dest);

        //catalogs
        model.addAttribute("catalogs", strategyCatalogService.queryCatalogByDestId(id));
        //strategyDetails
        model.addAttribute("strategyDetails", strategyDetailService.getDetailTop3(id));
        return "destination/guide";
    }


    //travels
    @RequestMapping("travels")
    public String guide(@ModelAttribute("qo") TravelQuery qo, Model model) {
        model.addAttribute("pageInfo", travelService.queryForList(qo));
        return "destination/travelTpl";
    }
}